/**
	Vector2
		@author  Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
		@version 2013.04.19
**/
function Vector2(x, y)
{
	
	this.x = x;
	this.y = y;
	
	this.add = function(value)
	{
		if (value instanceof Vector2)
		{
			this.x += value.x;
			this.y += value.y;
			return;
		}
		this.x += value;
		this.y += value;
	}
	
	this.divide = function(value)
	{
		if (value instanceof Vector2)
		{
			this.x /= value.x;
			this.y /= value.y;
			return;
		}
		var factor = 1 / value;
		this.x *= factor;
		this.y *= factor;
	}
	
	this.equal = function(value)
	{
		return (
			(this.x == value.x) &&
			(this.y == value.y)
		);
	}
	
	this.inequal = function(value)
	{
		return (
			(this.x != value.x) ||
			(this.y != value.y)
		);
	}
	
	this.length = function()
	{
		return Math.hypot(this.x, this.y);
	}
	
	this.lengthSquared = function()
	{
		return Math.hypotSquared(this.x, this.y);
	}
	
	this.multiply = function(value)
	{
		if (value instanceof Vector2)
		{
			this.x *= value.x;
			this.y *= value.y;
			return;
		}
		this.x *= value;
		this.y *= value;
	}
	
	this.negate = function()
	{
		this.x = -this.x;
		this.y = -this.y;
	}
	
	this.normalize = function()
	{
		var factor = 1 / this.length();
		this.x *= factor;
		this.y *= factor;
	}
	
	this.subtract = function(value)
	{
		this.x -= value.x;
		this.y -= value.y;
	}
	
}

Vector2.zero = function()
{
	return new Vector2(
		0,
		0
	);
}

Vector2.one = function()
{
	return new Vector2(
		1,
		1
	);
}

Vector2.unitX = function()
{
	return new Vector2(
		1,
		0
	);
}

Vector2.unitY = function()
{
	return new Vector2(
		0,
		1
	);
}

Vector2.add = function(value1, value2)
{
	return new Vector2(
		value1.x + value2.x,
		value1.y + value2.y
	);
}

Vector2.barycentric = function(value1, value2, value3, amount1, amount2)
{
    return new Vector2(
		Math.barycentric(value1.x, value2.x, value3.x, amount1, amount2),
		Math.barycentric(value1.y, value2.y, value3.y, amount1, amount2)
	);
}

Vector2.catmullRom = function(value1, value2, value3, value4, amount)
{
	return new Vector2(
		Math.catmullRom(value1.x, value2.x, value3.x, value4.x, amount),
		Math.catmullRom(value1.y, value2.y, value3.y, value4.y, amount)
	);
}

Vector2.clamp = function(value1, min, max)
{
    return new Vector2(
		Math.clamp(value1.x, min.x, max.x),
		Math.clamp(value1.y, min.y, max.y)
	);
}

Vector2.distance = function(value1, value2)
{
	return Math.hypot(value1.x - value2.x, value1.y - value2.y);
}

Vector2.distanceSquared = function(value1, value2)
{
	return Math.hypotSquared(value1.x - value2.x, value1.y - value2.y);
}

Vector2.divide = function(value1, value2)
{
	if (value2 instanceof Vector2)
		return new Vector2(
			value1.x / value2.x,
			value1.y / value2.y
		);
	var factor = 1 / value2;
	return new Vector2(
		value1.x * factor,
		value1.y * factor
	);
}

Vector2.dot = function(value1, value2)
{
    return (value1.x * value2.x) + (value1.y * value2.y);
}

Vector2.reflect = function(vector, normal)
{
	var factor = 2 * ((vector.x * normal.x) + (vector.y * normal.y));
	return new Vector2(
		vector.x - (normal.x * factor),
		vector.y - (normal.y * factor)
	);
}

Vector2.hermite = function(value1, tangent1, value2, tangent2, amount)
{
	return new Vector2(
		Math.hermite(value1.x, tangent1, value2.x, tangent2, amount),
		Math.hermite(value1.y, tangent1, value2.y, tangent2, amount)
	);
}

Vector2.lerp = function(value1, value2, amount)
{
    return new Vector2(
        Math.lerp(value1.x, value2.x, amount),
        Math.lerp(value1.y, value2.y, amount)
	);
}

Vector2.max = function(value1, value2)
{
    return new Vector2(
		(value1.x < value2.x) ? value2.x : value1.x, 
		(value1.y < value2.y) ? value2.y : value1.y
	);
}

Vector2.min = function(value1, value2)
{
    return new Vector2(
		(value1.x < value2.x) ? value1.x : value2.x,
		(value1.y < value2.y) ? value1.y : value2.y
	);
}

Vector2.multiply = function(value1, value2)
{
	if (value2 instanceof Vector2)
		return new Vector(
			value1.x * value2.x,
			value1.y * value2.y
		);
	return new Vector2(
		value1.x * value2,
		value1.y * value2
	);
}

Vector2.negate = function(value)
{
	return new Vector2(
		-value.x,
		-value.y
	);
}

Vector2.normalize = function(value)
{
	var factor = 1 / value.length();
    return new Vector2(
		value.x * factor,
		value.y * factor
	);
}

Vector2.smoothStep = function(value1, value2, amount)
{
    return new Vector2(
		Math.smoothStep(value1.x, value2.x, amount),
		Math.smoothStep(value1.y, value2.y, amount)
	);
}

Vector2.subtract = function(value1, value2)
{
	return new Vector2(
		value1.x - value2.x,
		value1.y - value2.y
	);
}